%%% ------------------------------------------------------------------ 
%%% Licensed under the Apache License, Version 2.0 (the 'License');
%%%  you may not use this file except in compliance with the License.
%%%  You may obtain a copy of the License at
%%%
%%%      http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Copyright (c) 2017 dwg <bypf2009@vip.qq.com>
%%%
%%%  Unless required by applicable law or agreed to in writing, software
%%%  distributed under the License is distributed on an 'AS IS' BASIS,
%%%  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%%  See the License for the specific language governing permissions and
%%%  limitations under the License.
%%%
%%% @doc  
%%% @author  dwg <'bypf2009@vip.qq.com'> 
%%% @copyright 2017 dwg <bypf2009@vip.qq.com>
%%% @end
%%% created|changed : 2017-05-22 11:51
%%% coding : utf-8 
%%% ------------------------------------------------------------------ 
-module(yangan_protocol_lib).
-author("dwg").

-export([
	decode/1,
	encode/1
]).

-include ("yangan.hrl").

decode(<<16#A1, FrameCount:4, FireAlarm:1, FaultAlarm:1, LBAlarm:1, TemAlarm:1, Temperature, Reserved, 16#E1>>) ->
	#yangan_status{
	   frame_count = FrameCount, 
	   fire_alarm = FireAlarm, 
	   fault_alarm = FaultAlarm,
	   lb_alarm = LBAlarm, 
	   tem_alarm = TemAlarm, 
	   temp_perature = Temperature,
	   reserved = Reserved
	};

decode(<<16#A2, FrameCount:4, FireAlarm:1, FaultAlarm:1, LBAlarm:1, TemAlarm:1, Temperature, Reserved, 16#E1>>) ->
	#yangan_status{
	   frame_count = FrameCount, 
	   fire_alarm = FireAlarm, 
	   fault_alarm = FaultAlarm,
	   lb_alarm = LBAlarm, 
	   tem_alarm = TemAlarm, 
	   temp_perature = Temperature,
	   reserved = Reserved
	};

%% HBI: HearbeatInterval
%% TDI: TempDetectInterval
%% TTL: TempThrl
%% TempThrH: TTH
decode(<<16#A3, Motetype, Mode1, HWVersion:4, Swversion:4, HBI:4, TDI:4, AlarmInterval, TTL, TTH, 16#E1>>) ->
	#yangan_parameters{
	   mote_type = Motetype, 
	   mode1 = Mode1,
	   hw_version = HWVersion, 
	   sw_version = Swversion, 
	   hbi = HBI,
	   tdi = TDI, 
	   alarm_interval = AlarmInterval, 
	   ttl = TTL,
	   tth = TTH
	}.

encode(#yangan_control{
	      silent_time = SilentTime,
	      stop_fire_alarm = StopFireAlarm, 
	      stop_fault_alarm = StopFaultAlarm,
	      stop_lb_alarm = StopLBAlarm, 
	      stop_temp_alarm = StopTempAlarm
	}) ->
	<<16#A4, SilentTime:4, StopFireAlarm:1, StopFaultAlarm:1, StopLBAlarm:1, StopTempAlarm:1>>;

encode(#yangan_command{
	      set_hbi = SetHBI,
	      set_tdi = SetTDI, 
	      set_tt = SetTT, 
	      restore = Restore,
	      reset = Reset, 
	      tdi = TDI, 
	      hbi = HBI, 
	      ttl = TTL,
	      tth = TTH, 
	      alarm_interval = AlarmInterval
	  }) ->
	<<
	  16#A5, 
	  0:3, SetHBI:1, SetTDI:1, SetTT:1, Restore:1, Reset:1,
	  TDI:4, HBI:4,
	  TTL,
	  TTH,
	  AlarmInterval,
	  16#E1
	>>.

% 	<<
% 	  16#A5, 
% 	  0:3, 0:1, 0:1, 0:1, 0:1, 1:1,
% 	  0:4, 0:4,
% 	  0,
% 	  0,
% 	  0,
% 	  16#E1
% 	>>.
% <<16#A4, 1:4, 1:1, 1:1, 1:1, 1:1>>.